package com.gxa.autoparts.admin.controller;



import com.gxa.autoparts.admin.service.AdminService;
import com.gxa.autoparts.admin.service.LogService;
import com.gxa.autoparts.admin.service.PermissionService;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.Admin;
import com.gxa.autoparts.core.pojo.Permission;
import com.gxa.autoparts.core.util.StringUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.UnknownHostException;
import java.util.Map;

@RequestMapping("/admin/adminManage")
@RestController
@Transactional(rollbackFor = Throwable.class)
public class AdminManageController {
    @Autowired
    private LogService logService;
    @PostMapping(value = "/login")
    public Map login(@RequestParam String name, @RequestParam String pwd) throws BusinessException, UnknownHostException {
        System.out.println(name+"-------------"+pwd);

        // shiro权限认证主体对象
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            // shiro权限认证类型
            UsernamePasswordToken upToken = new UsernamePasswordToken(name,pwd);

            try {
                // 调用realm认证用户权限
                subject.login(upToken);
            }/*
                return "login success";
            } catch (IncorrectCredentialsException ice) {
                return "用户名/密码不匹配！";
            } catch (LockedAccountException lae) {
                return "账户已被冻结！";
            } catch (UnknownAccountException uae) {
                return "账户不存在！";
            } catch (AuthenticationException ae) {
                return ae.getMessage();
            } */catch (Exception e){
                throw new BusinessException(ExceptionCode.LOGIN_FAILED);
            }
        }
        logService.insertLog("登录",name);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("登录成功");
        jsonResult.setData(adminService.selectByName(name));
        jsonResult.put("permission",adminService.selectPermissionByName(name));
        return jsonResult.getDataMap();
    }

    @Autowired
    private AdminService adminService;

    @PostMapping("/updateAdminPwd")
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id" ,required = true),
            @ApiImplicitParam(name = "oldPwd",value = "旧密码",required = true),
            @ApiImplicitParam(name = "newPwd1",value = "新密码",required = true),
            @ApiImplicitParam(name = "newPwd2",value = "确认密码",required = true)
    })
    public Map updateAdminPwd(Long id, String oldPwd,String newPwd1,String newPwd2) throws BusinessException, UnknownHostException {
        Admin admin = adminService.selectById(id);
        if (admin.getPwd().equals(oldPwd) && !StringUtil.isEmpty(newPwd1) && newPwd1.equals(newPwd2)){
            admin.setPwd(newPwd1);
            adminService.updateById(admin);
        } else {
            throw new BusinessException(ExceptionCode.WRONG_ADMINPWD);
        }
        logService.insertLog("修改密码",admin.getName());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改密码成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Map logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("退出登录成功");
        return jsonResult.getDataMap();
    }
    @Autowired
    private PermissionService permissionService;
    @PostMapping("/addpermission")
    @ApiOperation("添加权限表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true),
            @ApiImplicitParam(name = "name", value = "name", required = true),
            @ApiImplicitParam(name = "url", value = "url", required = true),
            @ApiImplicitParam(name = "desc", value = "desc", required = true)
    })
    public Map addPermission(Long pid,String name,String url,String desc){
        Permission permission = new Permission();
        permission.setDesc(desc);
        permission.setName(name);
        permission.setUrl(url);
        permission.setPid(pid);
        permissionService.insertPermission(permission);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加权限成功");
        return jsonResult.getDataMap();
    }
}
